package com.wan37.logic.dao.impl;

import com.wan37.logic.dao.SceneGoDao;
import com.wan37.logic.domain.entity.SceneGoDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author : luoyong
 * @date : 2020-06-01 19:18
 **/
@Transactional(propagation = Propagation.SUPPORTS, readOnly = false, rollbackFor = Exception.class)
@Repository
public class SceneGoDaoImpl implements SceneGoDao {

    @Autowired
    HibernateTemplate hibernateTemplate;

    @Override
    public boolean add(SceneGoDO sceneGoDO) {
        try {
            hibernateTemplate.save(sceneGoDO);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public boolean delete(Integer sceneIdGo) {
        try {
            hibernateTemplate.delete(sceneIdGo);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public boolean update(SceneGoDO sceneGoDO) {
        try {
            hibernateTemplate.update(sceneGoDO);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public SceneGoDO getSceneGoBySceneGoId(String sceneIdGo) {
        List<SceneGoDO> list = (List<SceneGoDO>) hibernateTemplate.find("from SceneGoDO where sceneIdGo = ?", sceneIdGo);
        if (!list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public List<SceneGoDO> getAll() {
        return (List<SceneGoDO>) hibernateTemplate.find("from SceneGoDO");
    }
}
